package com.jinkeen.xiaxie.activiity

import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.widget.TextView
import com.jinkeen.xiaxie.R
import com.jinkeen.xiaxie.base.BaseActivity
import com.jinkeen.xiaxie.base.MyApplication
import com.jinkeen.xiaxie.util.SystemInfo
import com.jinkeen.xiaxie.util.SystenInfoUtil.getIpAddress
import com.jinkeen.xiaxie.util.SystenInfoUtil.getMacAddressFromIp
import com.landicorp.android.eptapi.dock.DockEthernet
import com.landicorp.android.eptapi.dock.Docker
import com.landicorp.android.eptapi.dock.EthernetInfo
import com.landicorp.android.eptapi.dock.WifiDocker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.NetworkInterface

class DeviceInfoActivity : BaseActivity() {
    private var infoString = ""
    private var dockEthernet: DockEthernet? = null
    private var wifiDocker: WifiDocker? = null
    private val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
    private lateinit var battery: Intent
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_device_info)
        getDeviceInfo()
        infoString.replace("\\t", "\t")
        findViewById<TextView>(R.id.tv_info).text = infoString
    }

    private fun getDeviceInfo() {
        infoString += "archive:0\t"//不用管
        infoString += "电量百分比：" + deviceBatteryPer() + "\t"
        infoString += "是否正在充电:" + deviceChargingStatus() + "\t"
//        val  st=getDockEthernet()!!.open()
        infoString += "ip:" + getIpAddress(this) + "\t"
        infoString += "mac:" + getMacAddressFromIp(this) + "\t"
        infoString += "cpu占比:" + SystemInfo(this).getProcessCpuRate() + "\t"
        infoString += "cpu频率:" + SystemInfo(this).getCpu() + "\t"
        infoString += "是否使用有线网络" + SystemInfo(this).isUseWFI() + "\t"
        infoString += "是否使用3g" + SystemInfo(this).isUse3G() + "\t"
        infoString += "信号强度:" + SystemInfo(this).getTeleInfo()+"\t"

    }


    private fun deviceBatteryPer(): String {
        battery = this.registerReceiver(null, intentFilter)!!
        val level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, 1)
        val scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, 1)
        return "${level * 100 / scale}%"
    }

    //电池信息
    private fun deviceChargingStatus(): String {
        when (battery.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
            BatteryManager.BATTERY_STATUS_CHARGING -> return MyApplication.instance!!.getString(
                R.string.isCharging
            )
            BatteryManager.BATTERY_STATUS_FULL -> return MyApplication.instance!!.getString(
                R.string.fullCharging
            )
            BatteryManager.BATTERY_STATUS_DISCHARGING -> return MyApplication.instance!!.getString(
                R.string.disCharging
            )
            BatteryManager.BATTERY_STATUS_NOT_CHARGING -> return MyApplication.instance!!.getString(
                R.string.noCharging
            )
            BatteryManager.BATTERY_STATUS_UNKNOWN -> return MyApplication.instance!!.getString(
                R.string.unKnown
            )
            else -> return MyApplication.instance!!.getString(R.string.unKnown)
        }
    }

    fun getDockEthernet(): DockEthernet? {
        if (dockEthernet == null) {
            dockEthernet = getWifiDocker()!!.getDockEthernet(Docker.ETH)
        }
        return dockEthernet
    }

    fun getWifiDocker(): WifiDocker? {
        if (wifiDocker == null) {
            wifiDocker = WifiDocker.getInstance()
        }
        return wifiDocker
    }

    private fun getIp(): String {
        var ip = ""
        wifiDocker!!.getDockEthernet(object : DockEthernet.GetEthernetInfoListener() {
            override fun onSuccess(p0: EthernetInfo?) {
                ip = p0!!.ip
            }

            override fun onError(p0: Int) {
                ip = p0.toString()
            }

        }.toString())
        return ip
    }

    fun getNetworkIp(): String {
        val en = NetworkInterface.getNetworkInterfaces()
        val intf = en.nextElement()
        val addr = intf.inetAddresses
        addr.hasMoreElements()
        return addr.nextElement().hostAddress
    }


}